<?php
$link = '';
if($_POST){
    $str = file_get_contents($_POST['s']);
    $temp = array();
    preg_match('/yunData\.ISVIP([\s\S]*)/', $str, $temp);
    $str = $temp[1];
    $r = array();

    function getPat($name, $type = 'yunData\.'){
        return '/'.$type.$name.'\s*?=\s*?(.+)?;/';
    }

    function getVar($names, $str, &$r){
        $arr = array();
        for($i=0; $i< count($names); $i++){
            $name = $names[$i];
            if($name === '_context'){
                preg_match(getPat($name, ''), $str, $arr);
            }else{
                preg_match(getPat($name), $str, $arr);
            }

            $r[strtolower($name)] = isset($arr[1])? trim(trim(strval($arr[1]), " "), '"') :'';
        }

    }

    function do_post_request($url, $data, $optional_headers = null)
    {

        $data = http_build_query($data);
        $params = array('http' => array(
            'method' => 'POST',
            'content' => $data
        ));
        if ($optional_headers !== null) {
            $params['http']['header'] = $optional_headers;
        }
        $ctx = stream_context_create($params);
        $fp = @fopen($url, 'rb', false, $ctx);
        if (!$fp) {
            throw new Exception("Problem with $url, $php_errormsg");
        }
        $response = @stream_get_contents($fp);
        if ($response === false) {
            throw new Exception("Problem reading data from $url, $php_errormsg");
        }
        return $response;
    }
    getVar(array('MYBDSTOKEN', 'SIGN', 'TIMESTAMP', 'SHARE_ID', 'FILEINFO', 'SHARE_UK', 'SHARE_PUBLIC', 'SHARE_TIME'), $str, $r);
    $r['fileinfo'] = json_decode($r['fileinfo'], true);

    $fids = "[".number_format($r['fileinfo'][0]['fs_id'], 0, '', '')."]";
    $URL = "http://pan.baidu.com/api/sharedownload?sign={$r['sign']}&timestamp={$r['timestamp']}&bdstoken={$r['mybdstoken']}&channel=chunlei&clienttype=0&web=1&app_id={$r['fileinfo'][0]['app_id']}";
    $param = array(
        "encrypt" => 0,
        "product" => 'share',
        "uk"=> $r['share_uk'],
        "primaryid" => $r['share_id'],
        "fid_list" => $fids,
    );

    $q = do_post_request($URL, $param);
    $q = json_decode($q, true);

    if($q['errno'] == 0){
        $link = $q['list'][0]['dlink'];
    }else{
	$link = '您请求次连接次数太多,请更换其他链接再试';
        error_log(print_r($q, true), 3, '/tmp/bdwp.log');
    }



}


?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>百度网盘分享文件直链提取工具,无需登陆授权等操作</title>
<h3>百度网盘分享文件直链提取工具,无需登陆授权等操作</h3>
<i>同一链接提交次数不太多</i>
<form action="" method="post">

    <input name="s" value="" placeholder="请输入网盘文件分享地址"   style="width:500px;height:35px;">
    <input type="submit">
</form>
<br>
<input value="<?=$link?>"  style="width:500px;height:35px;"> 




